#include "tools.h"
#include "manager_view.h"
#include "manager_data.h"
#include "manager.h"
#include "book_service_view.h"

void book_menu(void)
{
	ManagerData m;
    m.load();
    system("clear");
	int id = 0;
	char password[20];
	cout << "请输入管理员账号：";
	cin >> id;getchar();
	cout << "请输入管理员密码：";
	char* pass_word = hide_passwd(password);
	vector<Manager>::iterator it;
    for(it = vecManager.begin();it != vecManager.end();it++)
    {
        if(id == it->id && 0 == strcmp(pass_word,it->password))
        {
            BookServiceView book_view_run;
			book_view_run.menu();
			return;
        }
    }
    cout << "账号密码错误" << endl;
    getchar();
    return;
}

void manager_menu(void)
{
	system("clear");
	int id = 0;
	char password[20];
	cout << "请输入超管账号：";
	cin >> id;getchar();
	cout << "请输入超管密码：";
	char* pass_word = hide_passwd(password);
	if(666666 == id && 0 == strcmp("abcdef",pass_word))
	{
		ManagerView manager_view_run;
		manager_view_run.menu();
		return;
	}
	else
	{
		cout << "账号密码错误" << endl;
		get_char();
		return;
	}
}

void get_char(void)
{
    cout << "****[q]返回****" << endl;
    while(getch() != 'q');
    return;
}

char* hide_passwd(char* passwd)
{
	char cur_passwd[20] = {};
	int char_num = 0;
	for(int i=0;i<15;i++)
	{
		if( 14 == i)
		{
			printf("\n");
			return NULL;
		}
		cur_passwd[i] = getch();
		
		if( '\n' == cur_passwd[i])
		{
			cur_passwd[i] = 0;
			strcpy(passwd,cur_passwd);
			printf("\n");
			char_num--;
			return passwd;
		}  
		if( 127 == cur_passwd[i] && char_num > 0)  //密码退格
		{
			char_num--;
			cur_passwd[i] = 0;
			--i;
			if( i >= 0 ) 
			{
				cur_passwd[i] = 0;
				--i;
			}
			printf("\b \b");
			continue;
		}
		else if(127 == cur_passwd[i]) 
		{
			cur_passwd[i] = 0;
			--i;
			if( i >= 0 ) 
			{
				cur_passwd[i] = 0;
				--i;
			}
		}
		else 
		{
			char_num++;
			printf("*");
		}
	}
	return passwd;
}

unsigned int generator_BookId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 100000;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[0];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[0];
}

unsigned int generator_BookListId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 10000;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[1];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[1];
}

unsigned int generator_ManagerId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 100;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[2];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[2];
}
unsigned int generator_UserId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 1000000;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[3];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[3];
}
